package com.example.admin.service.pay;

import com.example.admin.service.impl.PayService;
import com.lly835.bestpay.enums.BestPayTypeEnum;
import com.lly835.bestpay.model.PayResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;

@Controller
@RequestMapping("/pay")
public class pay {
    @Autowired
    private PayService payService;

    /**
     * 调用create()发起支付
     *
     * @param model
     * @param orderId
     * @param amount
     * @return
     */
    @GetMapping("/goodsPay")
    String nativePay(Model model,
                     @RequestParam("orderId") String orderId,
                     @RequestParam("amount") BigDecimal amount,
                     @RequestParam("bestPayTypeEnum") BestPayTypeEnum bestPayTypeEnum,
                     @RequestParam("goodsName") String goodsName) {
            /**
             * 如果微信NATIVE支付   参数分别是:订单号、支付金额、支付类型(bestPayTypeEnum=ALIPAY_WAP)
             */
        if (bestPayTypeEnum == BestPayTypeEnum.WXPAY_NATIVE) {/*微信native支付*/
            /*发起支付的返回值*/
            PayResponse response = payService.create(orderId, amount, bestPayTypeEnum,goodsName);
            model.addAttribute("url", response.getCodeUrl());
            return "wxPay_Native";
        } else if (bestPayTypeEnum == BestPayTypeEnum.ALIPAY_WAP) {/*支付宝h5支付*/
            return "nullPay";
        } else if (bestPayTypeEnum == BestPayTypeEnum.ALIPAY_PC) {/*支付宝pc支付*/
            /*发起支付的返回值*/
            System.out.println("第三次提交");
            PayResponse response = payService.create(orderId, amount, bestPayTypeEnum,goodsName);
            model.addAttribute("body", response.getBody());
            return "aliPay_Pc";
        } else if (bestPayTypeEnum == BestPayTypeEnum.ALIPAY_APP) {/*支付宝app支付*/
            System.out.println("支付宝app支付");
            return "nullPay";
        } else if (bestPayTypeEnum == BestPayTypeEnum.WXPAY_APP) {/*微信app支付*/
            return "nullPay";
        } else if (bestPayTypeEnum == BestPayTypeEnum.WXPAY_MINI) {/*微信小程序支付*/
            PayResponse response = payService.create(orderId, amount, bestPayTypeEnum,goodsName);

            System.out.println("微信小程序支付打印："+response);
            System.out.println("打印body"+response.getBody());
            return "nullPay";
        } else if (bestPayTypeEnum == BestPayTypeEnum.WXPAY_MP) {/*微信公众账号支付*/
            PayResponse response = payService.create(orderId, amount, bestPayTypeEnum,goodsName);

            System.out.println("打印body"+response.getBody());
            return "nullPay";
        } else if (bestPayTypeEnum == BestPayTypeEnum.WXPAY_MWEB) {/*微信H5支付*/
            PayResponse response = payService.create(orderId, amount, bestPayTypeEnum,goodsName);
            System.out.println("打印response："+response.getMwebUrl());
            return "nullPay";
        }
        return "nullPay";
    }
    /**
     * 接收异步通知  并处理异步通知   修改数据库订单状态
     * @param notifyData
     * @return
     */
    @PostMapping("/notify")
    @ResponseBody
    public String asyncNotify(@RequestBody String notifyData) {
        return payService.asyncNotify(notifyData);
    }

    /**
     * 支付完成后跳转到的页面，可以是订单列表，此方法支付完成后自动执行跳转
     * 注意：支付宝自动跳转，微信的跳转需要在wxPay_Native.html内修改
     * @return
     */
    @RequestMapping("/orderNo")
    @ResponseBody
    public String returnUrl(){
        return "欢迎来到订单列表页,您的订单已支付完成";
    }
}
